package com.hctforgreen.greenservice.db;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import com.hctforgreen.greenservice.utils.HctConstants;
/**
 * DB底层操作(调试维修列表页的栏目)
 * @author Tallen Zhai
 *
 */
public class DbColumnAdapter {
	private static final String DATABASE_NAME = HctConstants.DB_COLUMN_NAME;

    private static final int DBVERSION = HctConstants.DB_COLUMN_VERSION;
	private Context mContext;
	private DBHelper mHelper; 
	private SQLiteDatabase mDatabase;
	/**
	 * 创建调试栏目SQL
	 */
	private static final String CREATE_COLUMN_TABLE_SQL = 
			"create table " 
			+ HctConstants.DB_TABLE_NAME_COLUMN_LST
			+" (_id integer primary key autoincrement, "
			+ "id text, " 
			+ "icon text, "
			+ "name text" + ");"; 
	
	/**
	 * 构造函数
	 * @param context
	 */
	public DbColumnAdapter(Context context){
		mContext = context;
		mHelper = new DBHelper(mContext);
		mDatabase = mHelper.getWritableDatabase();
	}
	private class DBHelper extends SQLiteOpenHelper {

        public DBHelper(Context context) {
            super(context, DATABASE_NAME, null, DBVERSION);
        }

        @Override
        public void onCreate(SQLiteDatabase db) {
        	/**
        	 * 创建newVersion
        	 */
            db.execSQL(CREATE_COLUMN_TABLE_SQL);
        }

        
        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

        }
    }
	/**
	 * 执行
	 * @param sql
	 */
	public void execSQL(String sql) {
		mDatabase.execSQL(sql);
    }
	/**
	 * 执行
	 * @param sql
	 */
    public void execSQL(String sql, Object[] bindArgs) {
    	mDatabase.execSQL(sql, bindArgs);
    }

    /**
     * 插入
     * @param table
     * @param nullColumnHack
     * @param values
     * @return
     */
    public long insert(String table, String nullColumnHack, ContentValues values) {
        return mDatabase.insert(table, nullColumnHack, values);
    }

    /**
     * 查询
     * @param sql
     * @param bindArgs
     * @return
     */
    public Cursor rawQuery(String sql, String[] bindArgs) {
        return mDatabase.rawQuery(sql, bindArgs);
    }

    /**
     * 关闭
     */
    public void close() {
    	mDatabase.close();
    }

    public SQLiteDatabase getDatabase() {
        return mDatabase;
    }
}
